package com.synergy.stockflow.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.synergy.stockflow.model.Result;
import com.synergy.stockflow.product.entity.MaterialProductClassification;
import com.synergy.stockflow.product.entity.ProcessName;
import com.synergy.stockflow.product.service.MaterialProductClassificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/classify")
public class MaterialClassifcationController {
    @Autowired
    private MaterialProductClassificationService materialProductClassificationService;
    @GetMapping("/index")
    public Result<Page<MaterialProductClassification>>page(@RequestParam(name = "current", defaultValue = "1") int current,
                                                           @RequestParam(name = "size", defaultValue = "5") int size,
                                                           @RequestParam(name = "classifyName", defaultValue = "") String classifyName,
                                                           @RequestParam(name = "isEnabled", defaultValue = "") Integer isEnabled
    ){
        QueryWrapper<MaterialProductClassification> query = new QueryWrapper<>();
        if(classifyName != null && classifyName.length() > 0){
            query.lambda().like(MaterialProductClassification::getClassifyName,classifyName);
        }
        if(isEnabled != null){
            query.lambda().like(MaterialProductClassification::getIsEnabled,isEnabled);
        }

        Page<MaterialProductClassification>page=new Page<>(current,size);
        page=materialProductClassificationService.page(page,query);
        return Result.success(page);
    }

    @GetMapping("/Name")
        public Result<Page<MaterialProductClassification>>getName(){
        Page<MaterialProductClassification> page = new Page<>(1, 10000);
        List<MaterialProductClassification> list = materialProductClassificationService.list();
        page.setRecords(list);
        return Result.success(page);
    }
    @PostMapping("/insert")
    public Result insert(@RequestBody MaterialProductClassification materialProductClassification) {
        materialProductClassification.setClassifyId("");
        System.out.println(materialProductClassification);
        materialProductClassification.setUpdateTime(new Date());
        materialProductClassification.setCreateTime(new Date());
        materialProductClassificationService.save(materialProductClassification);
        return Result.success(materialProductClassification);

    }
    @PutMapping("/update")
    public Result update(@RequestBody MaterialProductClassification materialProductClassification) {
        materialProductClassification.setUpdateTime(new Date());
        materialProductClassificationService.updateById(materialProductClassification);
        return Result.success(materialProductClassification);
    }

    }


